package 寒假作业.LeetCode;

import java.util.HashMap;
import java.util.Map;

public class Num1160 {
    public int countCharacters(String[] words, String chars) {
        Map<Character, Integer> charMap = new HashMap<Character, Integer>();
        int length = chars.length();
        for (int i = 0; i < length; i++) {
            char c = chars.charAt(i);
            charMap.put(c, charMap.getOrDefault(c, 0) + 1);
        }
        int ans = 0;
        for (String word : words) {
            Map<Character, Integer> wordMap = new HashMap<Character, Integer>();
            int wordLength = word.length();
            for (int i = 0; i < wordLength; i++) {
                char c = word.charAt(i);
                wordMap.put(c, wordMap.getOrDefault(c, 0) + 1);
            }
            boolean isAns = true;
            for (int i = 0; i < wordLength; i++) {
                char c = word.charAt(i);
                if (charMap.getOrDefault(c, 0) < wordMap.getOrDefault(c, 0)) {
                    isAns = false;
                    break;
                }
            }
            if (isAns) {
                ans += word.length();
            }
        }
        return ans;
    }
}
